<?php include('fns_db_users.php'); ?>
<?php include('../views/layouts/_header.php'); ?>
<?php include('../views/layouts/_left.php'); ?>

<?php
function del_user_img($id){
	$result = get_user_by_id($id);
	$row = mysqli_fetch_assoc( $result );
	unlink("../images/users/thumbnails/".$row['img_path']);
	unlink("../images/users/".$row['img_path']);
}

function add_img_to_user($id){

	unset($imagename);

	if(!isset($_FILES) && isset($HTTP_POST_FILES))
	$_FILES = $HTTP_POST_FILES;

	//print_r($_FILES);
	if(!isset($_FILES['img_file'])){
		$error["img_file"] = "An image was not found.";
		echo $error;
		return;
	}

	$imagename = basename($_FILES['img_file']['name']);

	if(empty($error) && isset($imagename) && $imagename != null && $imagename != "")
	{
		$newimage = "../images/" . $imagename;
		$result = @move_uploaded_file($_FILES['img_file']['tmp_name'], $newimage);
		if(empty($result)){
			$error["result"] = "There was an error moving the uploaded file.";
		}
		resampimagejpg(450, 750, $newimage, "../images/users/".$imagename, 40);
		resampimagejpg(150, 250, $newimage, "../images/users/thumbnails/".$imagename, 20);
		unlink($newimage);
	}else{
		echo $error;

	}

	return $imagename;
}



if (isset($_GET['action']))
{
	$action = $_GET['action'];
	if ($action == '')
	{
		unset($action);
	}
}

switch ($action){

	case 'edit':
		if(isset($_SESSION['user'])){
			$user = $_SESSION['user'];
			$id = $user->id;
		}

		if (isset($_POST['user_name']))
		{
			$user_name = $_POST['user_name'];
			//			if ($user_name == '')
			//			{
			//				unset($user_name);
			//			}
		}

		if (isset($_POST['email']))
		{
			$email = $_POST['email'];
			//			if ($description == '')
			//			{
			//				unset($description);
			//			}
		}

		if (isset($_POST['fname']))
		{
			$fname = $_POST['fname'];
		}

		if (isset($_POST['lname']))
		{
			$lname = $_POST['lname'];
		}

		if (isset($_POST['city']))
		{
			$city = $_POST['city'];
		}

		if (isset($_POST['country']))
		{
			$country = $_POST['country'];
		}

		$imagename = add_img_to_user($id);

		if (isset($user_name))
		{
			if(isset($imagename) && $imagename != null){
				if($user->img_path != ''){
					del_user_img($user->id);
				}
				$result = update_user_with_imagename($id, $user_name, $fname, $lname, $email, $city, $country, $imagename);
			}else{
				$result = update_user_without_imagename($id, $user_name, $fname, $lname, $email, $city, $country);
			}

			if ($result == 'true')
			{
				echo "<p>User updated successfully!</p>";
				if(isset($imagename) && $imagename != null){
					$user = new user($id, $user_name, $fname, $lname, $email, $city, $country, $user->create_date, $imagename);
				}else{
					$user = new user($id, $user_name, $fname, $lname, $email, $city, $country, $user->create_date, $user->img_path);
				}
				$_SESSION['user'] = $user;
			}
			else
			{
				echo "<p>User not updated!</p>";
			}
		}
		else
		{
			echo "<p>You didn't enter information and the user cannot be updated.</p>";
		}

		break;


	default:
		echo "<p>Unexpected parameter passed!</p>";
}
?>

<?php include('../views/layouts/_footer.php'); ?>